package cn.iocoder.yudao.module.ems.service.equipment;

import cn.iocoder.yudao.module.ems.dal.dataobject.equipment.EquipmentDO;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.ems.controller.admin.equipment.vo.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.ems.convert.equipment.EquipmentConvert;
import cn.iocoder.yudao.module.ems.dal.mysql.equipment.EquipmentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_NOT_EXISTS;

/**
 * 设备 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class EquipmentServiceImpl implements EquipmentService {

    @Resource
    private EquipmentMapper equipmentMapper;

    @Override
    public Long createEquipment(EquipmentCreateReqVO createReqVO) {
        // 插入
        EquipmentDO equipment = EquipmentConvert.INSTANCE.convert(createReqVO);
        equipmentMapper.insert(equipment);
        // 返回
        return equipment.getId();
    }

    @Override
    public void updateEquipment(EquipmentUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateEquipmentExists(updateReqVO.getId());
        // 更新
        EquipmentDO updateObj = EquipmentConvert.INSTANCE.convert(updateReqVO);
        equipmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteEquipment(Long id) {
        // 校验存在
        this.validateEquipmentExists(id);
        // 删除
        equipmentMapper.deleteById(id);
    }

    private void validateEquipmentExists(Long id) {
        if (equipmentMapper.selectById(id) == null) {
            throw exception(EQUIPMENT_NOT_EXISTS);
        }
    }

    @Override
    public EquipmentDO getEquipment(Long id) {
        return equipmentMapper.selectById(id);
    }

    @Override
    public List<EquipmentDO> getEquipmentList(Collection<Long> ids) {
        return equipmentMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<EquipmentDO> getEquipmentPage(EquipmentPageReqVO pageReqVO) {
        return equipmentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EquipmentDO> getEquipmentList(EquipmentExportReqVO exportReqVO) {
        return equipmentMapper.selectList(exportReqVO);
    }

    @Override
    public List<EquipmentDO> getSimpleEquipments(EquipmentListReqVO reqVO) {
        return equipmentMapper.selectList(reqVO);
    }


    //
    @VisibleForTesting
    public void checkEquipmentListExists(Set<Long> ids) {
        if (ids == null) {
            return;
        }
        List<EquipmentDO> eqs = getEquipmentList(ids);
        if (eqs == null) {
            throw exception(EQUIPMENT_NOT_EXISTS);
        }
    }

    @Override
    public void updateEquipment(EquipmentDO equipmentDO) {
        // 校验存在
        this.validateEquipmentExists(equipmentDO.getId());
        // 更新
        equipmentMapper.updateById(equipmentDO);
    }
}
